﻿using System.Globalization;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Wordprocessing;

namespace OpenXmlEasy.Word
{
    public partial class WordEasy
    {

        public void InsertContent(string content)
        {
            var contents = content.Replace("\r\n", "ㄓ").Split('ㄓ');
            foreach (var c in contents)
            {
                InsertContent(c, 0);
            }
        }

        public void InsertContent(string content, float fontSize, bool right = false)
        {
            var paragraph1 = new Paragraph { RsidParagraphAddition = "005F46E8", RsidParagraphProperties = "005F46E8", RsidRunAdditionDefault = "005F46E8" };

            var fontSizeVal = ((int)(fontSize * 2)).ToString(CultureInfo.InvariantCulture);

            var paragraphProperties1 = new ParagraphProperties();
            {
                var indentation1 = new Indentation { FirstLine = "360", FirstLineChars = 200 };
                var paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();
                if (fontSize > 0)
                {
                    var fontSize1 = new FontSize { Val = fontSizeVal };
                    var fontSizeComplexScript1 = new FontSizeComplexScript { Val = fontSizeVal };
                    paragraphMarkRunProperties1.Append(new OpenXmlElement[] { fontSize1, fontSizeComplexScript1 });
                }
                paragraphProperties1.AppendChild(indentation1);
                paragraphProperties1.AppendChild(paragraphMarkRunProperties1);
            }
            if (right)
            {
                var justification1 = new Justification { Val = JustificationValues.Right };
                paragraphProperties1.AppendChild(justification1);
            }

            var run1 = new Run();
            {
                var runProperties1 = new RunProperties();
                if (fontSize > 0)
                {
                    var fontSize2 = new FontSize { Val = fontSizeVal };
                    var fontSizeComplexScript2 = new FontSizeComplexScript { Val = fontSizeVal };
                    runProperties1.Append(new OpenXmlElement[] { fontSize2, fontSizeComplexScript2 });
                }

                var text1 = new Text(content);

                run1.AppendChild(runProperties1);
                run1.AppendChild(text1);
            }
            paragraph1.AppendChild(paragraphProperties1);
            paragraph1.AppendChild(run1);
            _body.AppendChild(paragraph1);
        }

        public void InsertBreak()
        {
            var paragraph1 = new Paragraph { RsidParagraphAddition = "00E734D2", RsidRunAdditionDefault = "00E734D2" };

            var paragraphProperties1 = new ParagraphProperties();

            var sectionProperties1 = new SectionProperties { RsidR = "00E734D2", RsidSect = "001B619A" };
            var pageSize1 = new PageSize { Width = 11906U, Height = 16838U };
            var pageMargin1 = new PageMargin { Top = 1440, Right = 1080U, Bottom = 1440, Left = 1080U, Header = 851U, Footer = 992U, Gutter = 0U };
            var columns1 = new Columns { Space = "425" };
            var docGrid1 = new DocGrid { Type = DocGridValues.Lines, LinePitch = 312 };

            sectionProperties1.AppendChild(pageSize1);
            sectionProperties1.AppendChild(pageMargin1);
            sectionProperties1.AppendChild(columns1);
            sectionProperties1.AppendChild(docGrid1);

            paragraphProperties1.AppendChild(sectionProperties1);

            paragraph1.AppendChild(paragraphProperties1);

            _body.AppendChild(paragraph1);
        }

        public void InsertEmptyParagraph(int times = 1)
        {
            for (var i = 0; i < times; i++)
            {
                InsertParagraph(string.Empty);
            }
        }

        public void InsertMutiParagraph(string content, string ascii = null, float fontSize = 0, bool bold = false, bool center = false)
        {
            var contents = content.Replace("\r\n", "ㄓ").Split('ㄓ');
            foreach (var c in contents)
            {
                InsertParagraph(c, ascii, fontSize, bold, center);
            }
        }

        public void InsertParagraph(string content, string ascii = null, float fontSize = 0, bool bold = false, bool center = false)
        {
            var paragraph1 = new Paragraph { RsidParagraphAddition = "005F46E8", RsidParagraphProperties = "005F46E8", RsidRunAdditionDefault = "005F46E8" };

            var fontSizeVal = ((int)(fontSize * 2)).ToString(CultureInfo.InvariantCulture);

            var paragraphProperties1 = new ParagraphProperties();
            {
                var paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();
                if (bold)
                {
                    var bold1 = new Bold();
                    var boldComplexScript1 = new BoldComplexScript();
                    paragraphMarkRunProperties1.Append(new OpenXmlElement[] { bold1, boldComplexScript1 });
                }
                if (!string.IsNullOrEmpty(ascii))
                {
                    var runFonts1 = new RunFonts { Hint = FontTypeHintValues.EastAsia, Ascii = ascii, HighAnsi = ascii, EastAsia = ascii, ComplexScript = ascii };
                    paragraphMarkRunProperties1.AppendChild(runFonts1);
                }
                if (fontSize > 0)
                {
                    var fontSize1 = new FontSize { Val = fontSizeVal };
                    var fontSizeComplexScript1 = new FontSizeComplexScript { Val = fontSizeVal };
                    paragraphMarkRunProperties1.Append(new OpenXmlElement[] { fontSize1, fontSizeComplexScript1 });
                }
                paragraphProperties1.AppendChild(paragraphMarkRunProperties1);
            }
            if (center)
            {
                var justification1 = new Justification { Val = JustificationValues.Center };
                paragraphProperties1.AppendChild(justification1);
            }

            var run1 = new Run();
            {
                var runProperties1 = new RunProperties();
                if (bold)
                {
                    var bold2 = new Bold();
                    var boldComplexScript2 = new BoldComplexScript();
                    runProperties1.Append(new OpenXmlElement[] { bold2, boldComplexScript2 });
                }
                if (!string.IsNullOrEmpty(ascii))
                {
                    var runFonts2 = new RunFonts { Hint = FontTypeHintValues.EastAsia, Ascii = ascii, HighAnsi = ascii, EastAsia = ascii, ComplexScript = ascii };
                    runProperties1.AppendChild(runFonts2);
                }
                if (fontSize > 0)
                {
                    var fontSize2 = new FontSize { Val = fontSizeVal };
                    var fontSizeComplexScript2 = new FontSizeComplexScript { Val = fontSizeVal };
                    runProperties1.Append(new OpenXmlElement[] { fontSize2, fontSizeComplexScript2 });
                }

                var text1 = new Text(content);

                run1.AppendChild(runProperties1);
                run1.AppendChild(text1);
            }
            paragraph1.AppendChild(paragraphProperties1);
            paragraph1.AppendChild(run1);
            _body.AppendChild(paragraph1);
        }

    }
}
